package com.shigu.searchWord.service;

import com.opentae.data.mall.beans.UserSearchWord;
import com.opentae.data.mall.examples.UserSearchWordExample;
import com.opentae.data.mall.interfaces.UserSearchWordMapper;
import com.shigu.ListDto;
import com.shigu.PageResponse;
import com.shigu.constant.enums.PageUri;
import com.shigu.constant.enums.VipConstant;
import com.shigu.constant.service.SystemConstantService;
import com.shigu.searchWord.api.SearchWordService;
import com.shigu.searchWord.vo.HistorySearchVo;
import com.shigu.searchWord.vo.HotWordVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * create by  过儿  2019-06-04
 *
 */
@Service(value = "searchWordServiceImpl")
public class SearchWordServiceImpl implements SearchWordService {

    @Autowired
    UserSearchWordMapper userSearchWordMapper;
    @Autowired
    SystemConstantService systemConstantService;

    @Override
    public void insertWord(String wordText, Long mallUserId) {
        //用户为空，不记录
        if (mallUserId == null || StringUtils.isBlank(wordText)) {
            return;
        }

        UserSearchWordExample example = new UserSearchWordExample();
        example.createCriteria().andMallUserIdEqualTo(mallUserId).andTextEqualTo(wordText);
        example.setStartIndex(0);
        example.setEndIndex(1);
        example.setOrderByClause("last_search_time desc");
        List<UserSearchWord> list = userSearchWordMapper.selectByConditionList(example);
        if (list.size() > 0) {
            //使用过搜索，更新时间
            UserSearchWord word = list.get(0);
            word.setLastSearchTime(new Date());
            userSearchWordMapper.updateByPrimaryKeySelective(word);

        } else {
            //未使用过，插入
            UserSearchWord word = new UserSearchWord();
            word.setMallUserId(mallUserId);
            word.setLastSearchTime(new Date());
            word.setText(wordText);
            userSearchWordMapper.insertSelective(word);
        }
    }

    @Override
    public void deleteWord(Long wordId) {
        userSearchWordMapper.deleteByPrimaryKey(wordId);
    }

    @Override
    public ListDto<UserSearchWord> userSearchHistory(Long mallUserId) {

        UserSearchWordExample example = new UserSearchWordExample();
        example.createCriteria().andMallUserIdEqualTo(mallUserId);
        example.setOrderByClause("last_search_time desc");

        List<UserSearchWord> words = userSearchWordMapper.selectByExample(example);
        return new ListDto<>(false, words.size(), words);
    }

    @Override
    public PageResponse<HistorySearchVo> getHistorySearchWordList(Long mallUserId) {

        ListDto<UserSearchWord> dto = userSearchHistory(mallUserId);
        PageResponse<HistorySearchVo> response = new PageResponse<>();
        response.setData(dto.getData().stream().map(o -> {
            HistorySearchVo vo = new HistorySearchVo();
            vo.setWordId(o.getWordId());
            vo.setWordText(o.getText());
            return vo;
        }).collect(Collectors.toList()));

        return response.success();
    }

    @Override
    public PageResponse<HotWordVo> getHotSearchWordList() {
        Map<String, String> map = systemConstantService.selPageConstant(PageUri.system.toString());
        String hotSearch = map.get("hotSearch");
        String words = "";
        if (StringUtils.isNotBlank(hotSearch)) {
            //中文逗号替换成英文逗号
            words = hotSearch.replaceAll("，", ",");
        }
        List<String> data = Arrays.asList(words.split(","));
        PageResponse response = new PageResponse();
        //hotwordVo 有lombok 链式表达
        response.setData( data.stream().map(s -> new HotWordVo().setText(s) ).collect(Collectors.toList()));
        return response.success();
    }
}
